package rhplus.servico;

import java.sql.Date;
import java.sql.SQLException;
import javax.jdo.PersistenceManager;
import javax.jdo.annotations.IdGeneratorStrategy;
import javax.jdo.annotations.PersistenceCapable;
import javax.jdo.annotations.Persistent;
import javax.jdo.annotations.PrimaryKey;
import rhplus.acessoBD.PMF;

@PersistenceCapable
public class Factura {

    @PrimaryKey
    @Persistent(valueStrategy = IdGeneratorStrategy.IDENTITY)
    private Long id_factura;
    @Persistent
    private Long id_pagamento;
    @Persistent
    private Date data;

    public Factura(final Long id_pagamento, Date data) {
        this.id_pagamento = id_pagamento;
        this.data = data;
    }

    public Long getIdFactura() {
        return id_factura;
    }

    public Long getIdPagamento() {
        return id_pagamento;
    }

    public Date getData() {
        return data;
    }

    public static Factura devolve(final Long id_factura) throws SQLException, ClassNotFoundException, InstantiationException, IllegalAccessException {


        PersistenceManager pm = PMF.get().getPersistenceManager();

        String query = "select from " + Factura.class.getName()
                + " where id_factura == " + id_factura ;

        Factura factura = (Factura) pm.newQuery(query).execute();

        return factura;

    }

    public static Factura devolveDePagamento(final Long id_pagamento) throws SQLException, ClassNotFoundException, InstantiationException,
            IllegalAccessException {


        PersistenceManager pm = PMF.get().getPersistenceManager();

        String query = "select from " + Factura.class.getName()
                + " where id_pagamento == " + id_pagamento;

        Factura factura = (Factura) pm.newQuery(query).execute();

        return factura;

    }

    public static void insere(final Date data, final Long id_pagamento) throws SQLException, ClassNotFoundException, InstantiationException,
            IllegalAccessException {

        Factura factura = new Factura(id_pagamento, data);

        PersistenceManager pm = PMF.get().getPersistenceManager();

        try {
            pm.makePersistent(factura);
        } finally {
            pm.close();
        }


    }
}
